{% extends "questions/base.html" %}
{% from "layout/errorlist.html" import errorlist %}
{% from "upload/attachments.html" import attachments_form, attachment %}
{% from "includes/common_macros.html" import content_editor with context %}
{% from "wiki/includes/flag_form.html" import flag_form with context %}
{% from "questions/includes/questions.html" import troubleshooting_info %}
{% from "questions/includes/macros.html" import mark_spam_form, unmark_spam_form with context %}

{% set classes = 'answers' %}

{% set product_title = pgettext('DB: products.Product.title', product.title) if product else _('All Products') %}

{# L10n: {q} is the title of the question. #}
{% set title = _('{q} | {product} Support Forum')|f(q=question.title, product=product_title) %}

{% set crumbs = [(url('questions.home'), _('Support Forum')),
                 (url('questions.list', question.product_slug), product_title),
                 (None, question.title)] %}

{% set canonical_url = canonicalize(model_url=question.get_absolute_url()) %}

{% if answers.number > 1 %}
  {% set canonical_url = canonical_url|urlparams(page=answers.number) %}
{% endif %}

{% if robots_noindex %}
  {% set meta = (('robots', 'noindex'),) %}
{% endif %}

{% block above_main %}
  <h1>{{ _('Support Forum') }}</h1>

  {% if question.is_taken and question.taken_by != request.user %}
    <div class="taken-notice">
      {{ _('{user} is currently working on this problem.')|f(user=question.taken_by) }}
      <a href="#question-reply">{{ _('Post a reply') }}</a> |
      <a href="{{ url('questions.list', question.product.slug) }}">{{ _('View other questions that need attention.') }}</a>
    </div>
  {% endif %}
{% endblock %}

{% block content %}
  <article>
    <div id="question-{{ question.id }}" class="question grid_9" itemscope itemtype="http://schema.org/Question">
      {% if question.is_locked or question.is_archived %}
        <div class="notice">
          {% if question.is_locked and not question.is_archived %}
            {{ _('This thread was closed.') }}
          {% elif not question.is_locked and question.is_archived %}
            {{ _('This thread was archived.') }}
          {% else %}
            {{ _('This thread was closed and archived.') }}
          {% endif %}

          <a href="{{ url('questions.aaq_step1') }}">
            {{ _('Please ask a new question if you need help.') }}
          </a>
        </div>
      {% endif %}

      <h2 class="summary" itemprop="headline">{{ question.title }}</h2>

      <div class="question-meta">
        <ul class="cf">
          <li>
            {% if question.num_answers > 0 %}
              {# L10n: {n} is the number of replies. #}
              {{ ngettext('1 reply', '{n} replies', question.num_answers)|f(n=question.num_answers) }}
            {% else %}
              {{ _('No replies') }}
            {% endif %}
          </li>
          <li class="have-problem">
            {{ question.num_votes }}
            {% trans count=question.num_votes %}
              has this problem
              {% pluralize %}
              have this problem
            {% endtrans %}
          </li>
          {% if question.num_visits %}
            <li class="visits">
              {% trans count=question.num_visits %}
                {{count}} view
              {% pluralize %}
                {{count}} views
              {% endtrans %}
            </li>
          {% endif %}
          {% if question.last_answer %}
            <li>
              <a href="{{ question.last_answer.get_absolute_url() }}">
                {{ _('Last reply by {user}')|f(user=display_name(question.last_answer.creator)) }}
              </a>
              <span class="last-answer-date">
                <time itemprop="dateModified" datetime="{{ question.last_answer.created }}">{{ question.last_answer.created|timesince }}</time>
              </span>
            </li>
          {% endif %}
          {% if question.is_spam %}
            <li class="marked-as-spam">
              {% trans marked_by=question.marked_as_spam_by, marked_time=question.marked_as_spam %}
                Marked as spam by {{ marked_by }} at {{ marked_time }}
              {% endtrans %}
            </li>
          {% endif %}
        </ul>
      </div>

      <div class="user-meta">
        <div class="avatar">
          <a href="{{ profile_url(question.creator, request.user == question.creator) }}">
            <img src="{{ profile_avatar(question.creator) }}" height="48" width="48" alt="{{ display_name(question.creator) }}"/>
          </a>
        </div>
        <div class="asked-by">
          <a class="author-name" href="{{ profile_url(question.creator, request.user == question.creator) }}">{{ display_name(question.creator) }}</a>
        </div>
        <div class="asked-on">
          {{ _('Posted') }}<br>
          <time itemprop="dateCreated" datetime="{{ question.created }}">{{ datetimeformat(question.created) }}</time>
        </div>
      </div>

      <div class="main-content">
        <div itemprop="text">{{ question.content_parsed|safe }}</div>
        {# Note: this is not vulnerable to injection because Jinja autoescapes the strings. #}
        <div class="content-raw">{{ question.content }}</div>
        {% if question_images %}
          <div class="attachments-title">
            {{ _('Attached screenshots') }}
          </div>
          <div class="question-attachments attachments-list">
          {% for image in question_images %}
            {{ attachment(image, user, False) }}
          {% endfor %}
          </div>
        {% endif %}

        {% if question.updated_by %}
          <p class="edited">
            {{ _('Modified {datetime} by {name}')|fe(name=display_name(question.updated_by), datetime=datetimeformat(question.updated, format='longdatetime')) }}
          </p>
        {% endif %}

        {% if question.solution %}
          {% set solver = question.solver %}
          <div class="solution">
            <h3{% if solver %} title="{{ _('Solution chosen by {user}')|f(user=display_name(solver)) }}"{% endif %}>{{ _('Chosen solution') }}</h3>
            <div class="reply">
              {{ question.solution.content_parsed|safe }}
              <a class="read-more" href="{{ question.solution.get_absolute_url() }}">{{ _('Read this answer in context') }}</a>
              <span class="helpful-count">{{ question.solution.num_helpful_votes }}</span>
            </div>
          </div>
        {% endif %}
      </div>

      {% if question.editable %}
        <div class="question-tools cf">
          {% if not question.has_voted(request) %}
            <div class="me-too">
              <form action="{{ url('questions.vote', question_id=question.id) }}" method="post">
                <button class="btn big" type="submit">{{ _('I have this problem, too') }}</button>
              </form>
            </div>
          {% endif %}
          <a class="quoted-reply" href="#question-reply" data-content-id="question-{{ question.id }}">{{ _('Quote') }}</a>
        </div>
      {% endif %}
    </div>

    <div class="grid_3">
      <ul class="sidebar-nav sidebar-folding tight">
        <li id="question-tools">
          <span>{{ _('Question tools') }}</span>
          <ul class="sidebar-nav tight">
            {% if question.allows_edit(user) %}
              <li class="edit">
                <a href="{{ url('questions.edit_question',
                        question.id) }}">{{ _('Edit this question') }}</a>
              </li>
            {% endif %}
            {% if question.allows_delete(user) %}
              <li class="delete">
                <a class="delete" href="{{ url('questions.delete',
                        question.id) }}">{{ _('Delete this question') }}</a>
              </li>
            {% endif %}
            {% if question.allows_lock(user) %}
              <li class="lock">
                <a data-form="lock-form" data-type="submit">
                    {% if question.is_locked %}{{ _('Unlock this question') }}
                    {% else %}{{ _('Lock this question') }}{% endif %}</a>
                <form id="lock-form" class="lock" action="{{ url('questions.lock', question.id) }}" method="post">
                  {% csrf_token %}
                </form>
              </li>
            {% endif %}
            {% if question.allows_archive(user) %}
              <li class="archive">
                {% if question.age < 180 * 24 * 60 * 60 %}
                  <a data-form="archive-form" data-type="submit">
                    {% if question.is_archived %}
                      {{ _('Unarchive this question') }}
                    {% else %}
                      {{ _('Archive this question') }}
                    {% endif %}
                  </a>
                  <form id="archive-form" class="archive" action="{{ url('questions.archive', question.id) }}" method="post">
                    {% csrf_token %}
                  </form>
                {% endif %}
              </li>
            {% endif %}
            <li class="email">
              {% if is_watching_question %}
                <a data-form="unwatch-form" data-type="submit">{{ _('Stop email updates') }}</a>
                <form id="unwatch-form" class="unwatch" action="{{ url('questions.unwatch', question.id) }}" method="post">
                  {% csrf_token %}
                </form>
              {% else %}
                <a id="email-subscribe-link" href="#email-subscribe">{{ _('Get email updates') }}</a>
                {% include "questions/includes/email_subscribe.html" %}
              {% endif %}
            </li>
            <li class="rss">
              <a href="{{ url('questions.answers.feed', question.id) }}">{{ _("Subscribe to feed") }}</a>
            </li>
          </ul>
        </li>
      </ul>

      <form method="post" action="{{ url('questions.edit_details', question.id) }}">
        <ul class="sidebar-nav sidebar-folding tight">
          <li id="question-details">
            <span>{{ _('Question details') }}</span>
            <div class="folder">
              {% if product or (user and user.has_perm('questions.change_question')) %}
                <div class="sidebox tight condensed">
                  <span class="title">{{ _('Product') }}</span>
                  {% if product %}
                    <span class="detail">{{ pgettext('DB: products.Product.title', product.title) }}</span>
                  {% endif %}
                  {% if user and user.has_perm('questions.change_question') %}
                    <select id="details-product" name="product">
                      {% for p in all_products %}
                        <option {% if product and p.id == product.id %}selected="selected"{% endif %} value="{{ p.id }}" data-url="{{ url('products.product', p.slug) }}">{{ p.title }}</option>
                      {% endfor %}
                    </select>
                  {% endif %}
                  </span>
                </div>
              {% endif %}

              {% if topic or (user and user.has_perm('questions.change_question')) %}
                <div class="sidebox tight condensed">
                  <span class="title">{{ _('Topic') }}</span>
                  {% if topic %}
                    <span class="detail">{{ pgettext('DB: products.Topic.title', topic.title) }}</span>
                  {% endif %}
                  {% if user and user.has_perm('questions.change_question') %}
                    <select id="details-topic" name="topic">
                      {% for t in all_topics %}
                        <option {% if topic and t.id == topic.id %}selected="selected"{% endif %} value="{{ t.id }}">{{ t.title }}</option>
                      {% endfor %}
                    </select>
                  {% endif %}
                </div>
              {% endif %}

              {% if user and user.has_perm('questions.change_question') %}
                <div class="sidebox tight condensed" id="question-locale">
                  <span class="title">{{ _('Locale') }}</span>
                  <span class="detail">{{ settings.LANGUAGES_DICT[question.locale.lower()] }}</span>
                  <select name="locale">
                    {% for lang in AAQ_LANGUAGES %}
                      <option {% if question.locale == lang %}selected="selected"{% endif %} value="{{ lang }}">{{ settings.LANGUAGES_DICT[lang.lower()] }}</option>
                    {% endfor %}
                  </select>
                </div>
              {% endif %}

              <div class="sidebox tight {% if user and user.has_perm('questions.change_question') %}condensed{% endif %}" id="system-details">
                <span class="title">{{ _('System Details') }}</span>
                {% block system_info %}
                  <ul class="system">
                    {% if question.metadata.os %}
                      {% set os = question.metadata.os %}
                      {% set os_lower = os.lower() %}
                      <li class="{% if os_lower.find('mac') >= 0 %}mac{% elif os_lower.find('linux') >= 0 %}linux{% elif os_lower.find('win') >= 0 %}windows{% endif %}">{{ os }}</li>
                    {% endif %}
                    {% if question.metadata.ff_version %}
                      <li class="ff">Firefox {{ question.metadata.ff_version }}</li>
                    {% endif %}
                    {% if question.metadata.device %}
                      <li>{{ _('Device:') }} {{ question.metadata.device }}</li>
                    {% endif %}
                  </ul>
                {% endblock %}
                <div><a id="show-more-details" href="#more-system-details">{{ _('More system details') }}</a></div>
              </div>

              {% if user and user.has_perm('questions.change_question') %}
                <div class="sidebox tight">
                  <a href="#" id="details-edit">{{ _('Edit details') }}</a>
                  <button id="details-submit" type="submit" class="btn btn-submit">{{ _('Save changes') }}</button>
                </div>
              {% endif %}
            </div>
          </li>
        </ul>
        {% csrf_token %}
      </form>

      <section id="more-system-details" class="kbox" title="{{ _('Additional System Details') }}" data-target="#show-more-details" data-modal="true" data-id="more-system-details-kbox" data-close-on-out-click="true">
        <h1>{{ _('Additional System Details') }}</h1>
        {% if question.metadata.sites_affected %}
          <h2>{{ _('Sites Affected') }}</h2>
          <p>{{ question.metadata.sites_affected }}</p>
        {% endif %}
        {% if question.metadata.crash_id %}
          <h2>{{ _('Crash ID') }}</h2>
          <p>{{ question.metadata.crash_id }}</p>
        {% endif %}
        {% if question.metadata.frequency %}
          <h2>{{ _('This happened') }}</h2>
          <p>{{ frequencies[question.metadata.frequency] }}</p>
        {% endif %}
        {% if question.metadata.started %}
          <h2>{{ _('This started when...') }}</h2>
          <p>{{ question.metadata.started }}</p>
        {% endif %}
        {% if question.metadata.plugins %}
          <h2>{{ _('Installed Plug-ins') }}</h2>
          <div class="plugins">
            {{ question.metadata.plugins|wiki_to_html }}
          </div>
        {% endif %}
        {{ troubleshooting_info(question) }}
        <div class="bottom-close">
          <a href="#close" class="kbox-cancel">{{ _('Close') }}</a>
        </div>
      </section>

      <ul class="sidebar-nav sidebar-folding tight">
        <li id="tags-list">
          <span>{{ _('Tags') }}</span>
          <div class="folder">
            {% set tags = question.my_tags %}
            {% if tags or can_tag %}
              <div class="sidebox tight cf" id="tags">
                <div class="tags"{% if can_tag %} data-tag-vocab-json="{{ tag_vocab() }}"{% endif %}{% if can_create_tags %} data-can-create-tags="1"{% endif %}>
                  {% if can_tag %}
                    <form action="{{ url('questions.remove_tag', question.id) }}"
                          data-action-async="{{ url('questions.remove_tag_async', question.id) }}"
                          method="POST"
                          class="remove-tag-form">
                      {% csrf_token %}
                  {% endif %}
                  <ul class="tag-list cf{% if not can_tag %} immutable{% endif %}">
                    {% for tag in tags %}
                      <li class="tag">{# -#}
                        <a class="tag-name" href="{{ url('questions.list', product.slug if product else 'all')|urlparams(tagged=tag.slug) }}">{{ tag }}</a>
                        {%- if can_tag -%}
                          <button type="submit" class="remover" data-type="submit" data-name="remove-tag-{{ tag }}">&#xd7;</button>
                        {%- endif -%}
                      </li>
                    {% endfor %}
                  </ul>
                  {% if can_tag %}
                    </form>
                  {% endif %}

                  {% if can_tag %}
                    {% if tag_adding_error %}
                      <p class="tag-error-message">{{ tag_adding_error }}</p>
                    {% endif %}
                    <form action="{{ url('questions.add_tag', question.id) }}"
                          data-action-async="{{ url('questions.add_tag_async', question.id) }}"
                          method="POST"
                          class="tag-adder">
                      {% csrf_token %}
                      <input type="text" name="tag-name" size="12" maxlength="100"
                             class="searchbox autocomplete-tags {% if tag_adding_error %} invalid{% endif %}"
                             value="{{ tag_adding_value }}" />

                      <input class="btn" type="submit" value="{{ _('Add') }}" />
                    </form>
                  {% endif %}
                </div>
              </div>
            {% endif %}
          </div>
        </li>
      </ul>

      {% if related_documents or related_questions %}
        <ul class="sidebar-nav sidebar-folding tight">
          <li id="related-content">
            <span>{{ _('Related') }}</span>
            <ul class="sidebar-nav tight">
              {% for q in related_questions %}
                <li class="related-question">
                  <a href="{{ q.url[0] }}">{{ q.question_title[0] }}</a>
                </li>
              {% endfor %}
              {% for d in related_documents %}
                <li class="related-document">
                  <a href="{{ d.url[0] }}">{{ d.document_title[0] }}</a>
                </li>
              {% endfor %}
            </ul>
          </li>
        </ul>
      {% endif %}

      {% if user.has_perm('users.screen_share') %}
        <form class="screen-share-form cf" method="post" action="{{ url('questions.screen_share', question.id) }}">
          {% csrf_token %}
          <a href="#" class="btn btn-submit" data-type="submit">{{ _('Invite to share screen') }}</a>
        </form>
      {% endif %}

      {% if user.has_perm('flagit.can_moderate') %}
        {% if question.is_spam %}
          {{ unmark_spam_form(question_id=question.id) }}
        {% else %}
          {{ mark_spam_form(question_id=question.id) }}
        {% endif %}
      {% endif %}

      {% if question.allows_flag(user) %}
        {{ flag_form(url('questions.flag', question.id)) }}
      {% endif %}
    </div>

    {% if answers.object_list %}
      {% set helpful_replies = question.helpful_replies %}
      {% for answer in answers.object_list %}
        {% include "questions/includes/answer.html" %}
      {% endfor %}
      <div class="grid_12">
        {{ answers|paginator }}
      </div>
    {% endif %}
  </article>
  <div id="question-reply" class="grid_9 suffix_3">
    {% if request.limited %}
      <div class="warning-box">
        {% trans url=url('forums.threads', 'contributors') %}
          In order to prevent spam, we are limiting the number of answers that
          can be posted within a short period of time. If you want to post more
          please create a post on the <a href="{{ url }}">contributors
          forum</a> requesting that you be white-listed.
        {% endtrans %}
      </div>
    {% endif %}

    {% if question.allows_new_answer(user) %}
      <div class="answer-tools nopadding">
        <h3>{{ _('Post a Reply') }}</h3>
      </div>
      <form action="{{ url('questions.reply', question_id=question.id) }}#question-reply" method="post" enctype="multipart/form-data" data-take-question-url="{{ url('question-take', question.pk) }}">
        {% csrf_token %}
        {{ errorlist(form) }}

        <div class="main-content">
          {{ content_editor(form.content) }}

          {% if question.needs_info %}
            <label>
              <input type="checkbox" name="clear_needsinfo" {% if user == question.creator %}checked="checked"{% endif %}>
              {{ _('I am providing the requested information') }}
            </label>
          {% else %}
            <label>
              <input type="checkbox" name="needsinfo">
              {{ _('Needs more information') }}
            </label>
          {% endif %}
        </div>

        {{ attachments_form('auth.User', user.pk, images, settings, user) }}

        <div class="answer-tools nopadding">
          <button type="submit" class="btn btn-submit big">{{ _('Post Reply') }}</button>
          <input type="submit" id="preview" name="preview" value="{{ _('Preview Reply') }}"
            data-preview-url="{{ url('questions.answer_preview_async') }}"
            data-preview-container-id="answer-preview"
            data-preview-content-id="id_content" />
        </div>
      </form>
    {% elif question.editable and not user.is_authenticated() %}
      <div class="question-tools nopadding">
        {% if request.LANGUAGE_CODE in AAQ_LANGUAGES %}
          {% set ask_url = url('questions.aaq_step1') %}
        {% else %}
          {% set ask_url = url('wiki.document', 'get-community-support') %}
        {% endif %}

        <div class="cf">
          {% if answers.object_list and not question.has_voted(request) %}
            {# Only show the button again if there are answers in between #}
            <div class="me-too">
              <form action="{{ url('questions.vote', question_id=question.id) }}" method="post">
                <button class="btn big" type="submit">{{ _('I have this problem, too') }}</button>
              </form>
            </div>
          {% endif %}
          <a class="btn" href="{{ ask_url }}">{{ _('Ask a question') }}</a>
        </div>

        <p>
          {{ _('You must <a href="{login_url}">log in to your account</a> to reply to posts. Please <a href="{ask_url}">start a new question</a>, if you do not have an account yet.')|fe(login_url=url('users.login'), ask_url=ask_url) }}
        </p>
      </div>
    {% endif %}

    <section id="answer-preview">
      {% if answer_preview %}
        {% include "questions/includes/answer_preview.html" %}
      {% endif %}
    </section>
  </div>

{% endblock %}

{% block side %}
{% endblock %}
